let provinceObj,cityObj;
var provinceArray = [{id:"1",name:"四川"},
                     {id:"2",name:"湖南"},
                     {id:"3",name:"云南"}];
var cityArray = [{id:"1",name:"成都",idP:"1"},
    {id:"2",name:"南充",idP:"1"},{id:"3",name:"巴中",idP:"1"},
    {id:"4",name:"长沙",idP:"2"},
    {id:"5",name:"株洲",idP:"2"},{id:"6",name:"衡阳",idP:"2"},
    {id:"7",name:"丽江",idP:"3"},
    {id:"8",name:"大理",idP:"3"},{id:"9",name:"洱海",idP:"3"}, ];
window.onload = function () {
    provinceObj = document.getElementById("province");
    cityObj=document.getElementById("city");

    for(var i=0;i<provinceArray.length;i++){
        var pro = provinceArray[i];
        var option = document.createElement("option");
        option.innerHTML=`${pro.name}`;
        provinceObj.appendChild(option);
    }
    provinceObj.onchange=function(){
        //这个地方应该是按name的值获取该数组的下标元素的，没找到怎么获取
        for(var i=0;i<provinceArray.length;i++){
            if(provinceObj.value == provinceArray[i].name){
                changeCity(provinceArray[i]);
            }
        }
    }
    changeCity(provinceArray[0]);
}
function changeCity(ctObj) {
    cityObj.innerHTML="";
    for(var i=0;i<cityArray.length;i++){
        var city = cityArray[i];
        if(city.idP == ctObj.id){
            var option = document.createElement("option");
            option.innerHTML=`${city.name}`;
            cityObj.appendChild(option);
        }
    }
}